package com.fofia.iotweb.web.controllers.iot;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.StaticLog;
import com.fofia.iotweb.framework.core.BaseController;
import com.fofia.iotweb.framework.core.RequestHelper;
import com.fofia.iotweb.web.domain.service.GatewayService;
import com.fofia.iotweb.web.dto.DataTableDto;
import com.fofia.iotweb.web.dto.JsonResult;
import org.noear.solon.annotation.*;
import org.noear.solon.auth.annotation.AuthPermissions;
import org.noear.solon.core.handle.ModelAndView;


@Controller
@Mapping("iot/gateway")
public class GatewayController extends BaseController {

    @Inject
    private GatewayService gatewayService;
    @Inject
    private RequestHelper requestHelper;


    @AuthPermissions("#")
    @Get
    @Mapping("index")
    public ModelAndView index(){

        String today= DateUtil.today();
        return view().put("stime",today).put("etime",today);
    }

    /**
     * 分页数据，时间降序
     * @return
     */

    @AuthPermissions("#")
    @Post
    @Mapping("getHistoryPageList")
    public DataTableDto getHistoryPageList() {

        String gatewayId=requestHelper.getString("gatewayId","");
        int page=requestHelper.getInt("pageNumber",1);
        int pageSize=requestHelper.getInt("pageSize",10);
        String stime=requestHelper.getString("queryStartDay","");
        String etime=requestHelper.getString("queryEndDay","");
        String lastPageUploadTime=requestHelper.getString("lastPageUploadTime","");
        //StaticLog.info("来自Ip:{},getPageList:{} {} {} {},{}",requestHelper.getIp(),gatewayId,page,pageSize,stime,etime);
        if(StrUtil.isEmpty(gatewayId)){
            return JsonResult.table("请输入标签id");
        }
        if(StrUtil.isEmpty(stime)){
            return JsonResult.table("请输入开始时间");
        }
        if(StrUtil.isEmpty(etime)){
            return JsonResult.table("请输入结束时间");
        }

        try{

            stime=StrUtil.format("{} 00:00:00",stime);
            etime=StrUtil.format("{} 23:59:59",etime);
            var list=gatewayService.getPageList(gatewayId,page,pageSize,stime,etime);
            return list;

        }catch (Exception ex){
            StaticLog.error(ex);
        }

        return JsonResult.table();
    }
}
